<?php
require_once ("ConnectMysql.php");

fopen("config.json", "w");

$host = $_POST['host'];
$username = $_POST['username'];
$password = $_POST['password'];
$port = $_POST['port'];
$database = $_POST['database'];

file_put_contents("config.json",json_encode([
    "host" => $host,
    "username" => $username,
    "password" => $password,
    "port" => $port,
    "database" => $database
],true));

$conn = connect();

if ($conn->connect_error) {
    exit("数据库连接失败!"."(".$conn->connect_error.")");
}

$sql = "CREATE DATABASE $database";
$result = $conn->query($sql);

if (!$result) {
    exit("数据库创建失败!"."(".$conn->error.")");
}

$db = connectDB();

if ($db->connect_error) {
    exit("数据库连接失败!"."(".$db->connect_error.")");
}

$sql = file_get_contents("board.sql");
$sql = explode(';',$sql);
foreach ($sql as $s){
    if (trim($s) == ""){
        continue;
    }
    $db->query($s);
    if ($db->error) {
        exit("数据库迁移失败!".$db->error);
    }
}

echo 1;